import type { IBaseQuery, IChatRequest, IHistory } from '../types/chat'

export const getConfig = (name: string) => {
  return fetch(`/api/${name}/config`)
}

export const chat = (name: string, body: IChatRequest) => {
  return fetch(`/api/${name}/chat`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(body)
  })
}

export const streamChat = (name: string, body: IChatRequest, signal: AbortSignal) => {
  return fetch(`/api/${name}/streamChat`, {
    method: 'POST',
    signal: signal,
    headers: {
      'Data-Type': 'text/event-stream',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(body)
  })
}

export const uploadFileUrl = (name: string) => {
  return `/api/${name}/file`
}

export const disposeStream = (name: string, streamId: string) => {
  return fetch(`/api/${name}/streamChat?streamId=${streamId}`, {
    method: 'DELETE'
  })
}

export const getAgentList = (name: string, query: IBaseQuery) => {
  return fetch(`/api/${name}/agentList`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(query)
  })
}

export const getHistoryList = (name: string, query: IBaseQuery) => {
  return fetch(`/api/${name}/historyList`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(query)
  })
}

export const createHistory = (name: string, history: IHistory) => {
  return fetch(`/api/${name}/history`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(history)
  })
}
export const updateHistory = (name: string, history: IHistory) => {
  return fetch(`/api/${name}/history`, {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(history)
  })
}
export const deleteHistory = (name: string, id: string) => {
  return fetch(`/api/${name}/history?id=${id}`, {
    method: 'DELETE'
  })
}
